
<!-- 权限点等同于标记
 老李: ['emplyees', 'import']
  老王: ['employees', '其他页面', 'import', 'export']
  英文是在权限点页面管理的时候, 自己加入的(但是要注意数据的意思和一致性  -->
<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermFn(1,'0')">添加权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" type="text" @click="addPermFn(2,row.id)">添加</el-button>
              <el-button type="text" @click="redactPermFn(row.id)">编辑</el-button>
              <el-button type="text" @click="deleteFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog title="新增权限点" :visible.sync="showDialog" @close="btnCancel">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="confirmFn">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {

  data() {
    const validName = (rule, value, callback) => {
      let nameArr = this.perList.filter(item => item.pid === this.clickId).map(item => item.name)
      if (this.isEdit) {
        const obj = this.perList.find(item => item.id === this.clickId)
        nameArr = this.perList.filter(item => item.pid === obj.pid && item.id !== this.clickId).map(item => item.name)
      }
      nameArr.includes(value) ? callback(new Error(`权限点< ${value} >已存在`)) : callback()
    }
    const validcode = (rule, value, callback) => {
      let codeArr = this.perList.map(item => item.code)
      if (this.isEdit) {
        codeArr = this.perList.filter(item => item.id !== this.clickId).map(item => item.code)
      }
      codeArr.includes(value) ? callback(new Error(`${value} 权限标识已存在`)) : callback()
    }
    return {
      // 所有权限点列表数据(树形结构)
      permissionList: [],
      // 所有权限点列表数据(扁平jiegou)
      perList: [],

      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        type: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: validcode, trigger: 'blur' }
        ]
      },
      isEdit: false,
      clickId: ''

    }
  },
  created() {
    this.getPermissionListFn()
  },
  methods: {
    // 获取所有权限点
    async getPermissionListFn() {
      const res = await getPermissionListAPI()
      // console.log(res)
      // this.permissionList = res.data
      this.permissionList = tranListToTreeData(res.data, '0')
      this.perList = res.data
    },
    // 添加权限功能 (页面/子)
    addPermFn(type, clickId) { // type添加子的类型(1是页面权限点,2是按钮权限点)
      // chickId 点击行的id值(左上角按钮'0' 页面行权限点id 子pid=父id)
      this.isEdit = false
      this.showDialog = true
      // 存到表单对象里一起(现在要添加的权限点一些类型pid值)
      this.formData.type = type
      this.formData.pid = clickId
      this.clickId = clickId
    },
    // 点击编辑-弹出弹框(回显)
    async redactPermFn(perId) {
      this.isEdit = true
      this.clickId = perId
      this.showDialog = true
      const res = await getPermissionDetailAPI(perId)
      this.formData = res.data
    },
    // 添加/编辑权限点-弹窗关闭事件
    btnCancel() {
      this.formData = this.$options.data().formData
      setTimeout(() => {
        this.$refs.perForm.resetFields()
      }, 1000)
      this.showDialog = false
    },
    // 弹窗表单的确定->点击事件->保存权限点给后台
    confirmFn() {
      this.$refs.perForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.editPerHandlerFn() : this.addPerHandlerFn()
        }
      })
    },
    // 添加请求逻辑
    async addPerHandlerFn() {
      const res = await addPermissionAPI(this.formData)
      if (!res.success) return this.$message.error(res.message)
      this.getPermissionListFn()
      this.showDialog = false
    },
    // 编辑请求逻辑
    async editPerHandlerFn() {
      const res = await updatePermissionAPI(this.formData)
      if (!res.success) return this.$message.error(res.message)
      this.getPermissionListFn()
      this.showDialog = false
    },
    // 删除权限点
    async deleteFn(permId) {
      const res = await delPermissionAPI(permId)
      if (!res.success) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getPermissionListFn()
    }
  }
}
</script>

<style lang="scss" scoped />

